<?php
/**
 * ログライターリソース
 *
 * プログラムの実行状況をテキストファイルに
 * 出力するブートストラップリソース。
 *
 * LICENSE: Miracle Maple by Mirage is licensed
 * under a Creative Commons 表示-非営利-継承 2.1 日本 License.
 *
 * @category   bootstrap
 * @package    maple-story.jp
 * @version    8.0.0
 * @since      File available since Release 8.0.0
 * @copyright  Copyright 2011 Miracle Maple
 * @license    http://creativecommons.org/licenses/by-nc-sa/2.1/jp/     Creative Commons BY-NC-SA
 */
class Bootstrap_Resource_Log extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * リソース設定
     *
     * @ var array
     */
    protected $_options = array();

    /**
     * リソース設定 - 有効化
     *
     * ログライターを使用する場合は true。
     *
     * @var boolean
     */
    protected $_activate = false;
    
    /**
     * リソース設定 - ログ書き出しフォルダ
     *
     * デフォルトではアプリケーションディレクトリ配下の
     * logs フォルダを設定。
     *
     * @var string
     */
    protected $_basePath = '/logs';
    
    /**
     * リソース設定 - ログファイル名称
     *
     * @var string
     */
    protected $_fileEmerg  = '00_emergency.txt';
    protected $_fileAlert  = '01_alert.txt';
    protected $_fileCrit   = '02_critical.txt';
    protected $_fileErr    = '03_error.txt';
    protected $_fileWarn   = '04_warning.txt';
    protected $_fileNotice = '05_notice.txt';
    protected $_fileInfo   = '06_info.txt';
    protected $_fileDebug  = '07_debug.txt';

    /**
     * リソース起動
     *
     * オプションの設定を行い、ログライターが
     * 有効化されていれば Zend_Log を作成。
     *
     * @return void
     */
    public function init()
    {
        $options = $this->getOptions();
        
        if (array_key_exists('basePath', $options)) {
            $this->_options['basePath'] = $options['basePath'];
        } else if (defined('APPLICATION_PATH')) {
            $this->_options['basePath'] = DATA_PATH . $this->_basePath;
        } else {
            $this->_options['basePath'] = realpath($_SERVER['DOCUMENT_ROOT'] . '/../data' . $this->_basePath);
        }
        
        if (array_key_exists('activate', $options)) {
            $this->_options['activate'] = $options['activate'];
        } else {
            $this->_options['activate'] = $this->_activate;
        }
        
        if (array_key_exists('file', $options)) {
            if (is_array($options['file'])) {
                foreach ($options['file'] as $level => $fileName) {
                    $this->_options['file'][$level] = $fileName;
                }
            }
        }
        if (empty($this->_options['file'])) {
            $this->_options['file']['emerg'] = $this->_fileEmerg;
            $this->_options['file']['alert'] = $this->_fileAlert;
            $this->_options['file']['crit'] = $this->_fileCrit;
            $this->_options['file']['err'] = $this->_fileErr;
            $this->_options['file']['warn'] = $this->_fileWarn;
            $this->_options['file']['notice'] = $this->_fileNotice;
            $this->_options['file']['info'] = $this->_fileInfo;
            $this->_options['file']['debug'] = $this->_fileDebug;
        }

        if ($this->_options['activate'] == true) {
            $this->_setLogWriter();
        }
    }
    
    protected function _setLogWriter()
    {
        $logFiles = array();
        $logger = new Zend_Log();
        $filterLevel = 0;
        foreach ($this->_options['file'] as $level => $fileName) {
            $logFiles[$level] = $this->_options['basePath'] . '/' . $fileName;
            if (filesize($logFiles[$level]) >= 1024000) {        
                $oldFileName = explode('.', $fileName);
                $date = new DateTime();
                $newFileName = $oldFileName[0] . '_' . $date->format('YmdHis') . '.' . $oldFileName[1];
                rename($logFiles[$level], $this->_options['basePath'] . '/' . $newFileName);
            }
            $writer = 'log' . ucfirst($level);
            $$writer = new Zend_Log_Writer_Stream($logFiles[$level]);
            $filter = new Zend_Log_Filter_Priority($filterLevel++, '=');
            $$writer->addFilter($filter);
            $logger->addWriter($$writer);
        }
        Zend_Registry::set('APPLICATION_LOG', $logger);
    }
}
